package com.zhourong.mongodb.service.impl;

import javax.annotation.Resource;

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.zhourong.mongodb.model.User;
import com.zhourong.mongodb.service.IUserService;

@Service
public class UserServiceImpl implements IUserService {
	
	@Resource
	private MongoTemplate mongoTemplate;


	public User findUserById(Long id) {
		return mongoTemplate.findById(id, User.class);
	}

	public void insert(User user) {
		mongoTemplate.save(user);
	}

	public void delete(User user) {
		mongoTemplate.remove(user);
	}

	public void update(User user) {
		Query query = new Query(Criteria.where("userId").is(user.getUserId()));
		Update update = new Update();
		update.set("userId", user.getUserId());
		update.set("userName", user.getUserName());
		mongoTemplate.updateFirst(query, update, User.class);
	}
}